package cn.itcast.web.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/**
 * @author 黑马程序员
 */
public class JedisUtils {

    private static JedisPool jedisPool;

    static {

        //解析jedis.properties配置文件获取配置参数
        //ResourceBundle
        //介绍：专门用于解析properties文件
        //特点：只要根据类路径下properties文件的名字即可解析配置文件,文件名扩展名不可以写
        //语法：ResourceBundle ResourceBundle.getBundle("文件名");
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jedis");
        int maxTotal = Integer.parseInt(resourceBundle.getString("maxTotal"));
        int maxWaitMillis = Integer.parseInt(resourceBundle.getString("maxWaitMillis"));
        String host = resourceBundle.getString("host");
        int port = Integer.parseInt(resourceBundle.getString("port"));

        //创建配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal); //最大连接数
        config.setMaxWaitMillis(maxWaitMillis); //设置30秒

        //创建连接池对象
        jedisPool = new JedisPool(config,host,port);
    }

    //对外提供从连接池获取连接对象
    public static Jedis getJedis(){

        return jedisPool.getResource();
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        System.out.println(jedis);
    }
}
